package News.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import News.dao.BaseDao;
import News.dao.International_NewsDao;
import News.entity.International_News;

public class International_NewsDaoImpl implements International_NewsDao{
		private Connection connection;
		private PreparedStatement pstmt;
		private ResultSet rs;
		
	public List<International_News> findAllInternationalNews() {
		List<International_News> internews = new ArrayList<International_News>();
		try {
			connection = BaseDao.getConnection();
			String sql = "select NewsId,NewsContent,Time,NumOfRead from international_news";
			pstmt = connection.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next()){
				International_News InteN = new International_News();
				InteN.setNewsId(rs.getInt("NewsId"));
				InteN.setNewsContent(rs.getString("NewsContent"));
				InteN.setTime(rs.getString("Time"));
				InteN.setNumOfRead(rs.getInt("NumOfRead"));
				internews.add(InteN);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			BaseDao.closeAll(rs, pstmt, connection);
		}
		return internews;
	}

	public int delNewsId(int id) {
		int num = 0;
		try {
			connection = BaseDao.getConnection();
			String sql = "delete from International_News where newsid=?";
			pstmt = connection.prepareStatement(sql);
			pstmt.setInt(1, id);
			num = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			BaseDao.closeAll(rs, pstmt, connection);
		}
		return num;
	}


	public int addNews(int NewsId,String NewsContent,String Time,int NumOfRead) {
		int num = 0;
		try {
			connection = BaseDao.getConnection();
			String sql = "INSERT INTO international_news (Newsid,NewsContent,TIME,NumOfRead) VALUES(?,?,?,?)";
			pstmt = connection.prepareStatement(sql);
			pstmt.setInt(1, NewsId);
			pstmt.setString(2, NewsContent);
			pstmt.setString(3, Time);
			pstmt.setInt(4, NumOfRead);
			num = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			BaseDao.closeAll(rs, pstmt, connection);
		}
		return num;
	}

	public int updateNews(String NewsContent, String Time,int NumOfRead,int NewsId) {
		int num = 0;
		try {
			connection = BaseDao.getConnection();
			String sql = "UPDATE `international_news` SET newscontent=?,`time`=?,NumOfRead=? WHERE newsid=?";
			pstmt = connection.prepareStatement(sql);
			pstmt.setString(1, NewsContent);
			pstmt.setString(2, Time);
			pstmt.setInt(3, NumOfRead);
			pstmt.setInt(4, NewsId);
			num=pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			BaseDao.closeAll(rs, pstmt, connection);
		}
		return num;
	}

	public List<International_News> getPageNewsList(int CurrNo, int PageSize) {
		List<International_News> List = new ArrayList<International_News>();
		try {
			connection = BaseDao.getConnection();
			String sql = "select newsid,newscontent,time,numofread from International_News where 1=1 limit ?,?";
			pstmt = connection.prepareStatement(sql);
			pstmt.setInt(1, CurrNo-1*PageSize);
			pstmt.setInt(2, PageSize);
			rs = pstmt.executeQuery();
			while(rs.next()){
				International_News internew = new International_News();
				internew.setNewsId(rs.getInt("NewsId"));
				internew.setNewsContent(rs.getString("NewsContent"));
				internew.setTime(rs.getString("Time"));
				internew.setNumOfRead(rs.getInt("NumOfRead"));
				List.add(internew);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			BaseDao.closeAll(rs, pstmt, connection);
		}
		return List;
	}

	public int getTotalCount() {
		int num = 0;
		try {
			connection = BaseDao.getConnection();
			String sql = "select count(1) from International_News";
			pstmt = connection.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next()){
				num = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			BaseDao.closeAll(rs, pstmt, connection);
		}
		return num;
	}

}
